package com.csx.saas.platform.system.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.csx.basic.base.service.SuperServiceImpl;
import com.csx.basic.database.mybatis.conditions.Wraps;
import com.csx.saas.platform.system.dao.SystemRoleAuthorityMapper;
import com.csx.saas.platform.system.dto.SaveRoleResourceDTO;
import com.csx.saas.platform.system.entity.SystemRoleAuthority;
import com.csx.saas.platform.system.service.SystemRoleAuthorityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 角色的资源
 * </p>
 *
 * @author ChenSongXin
 * @date 2021-02-22
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class SystemRoleAuthorityServiceImpl extends SuperServiceImpl<SystemRoleAuthorityMapper, SystemRoleAuthority> implements SystemRoleAuthorityService {
    @Override
    public void saveResource(SaveRoleResourceDTO saveRoleResourceDTO) {
        //清除授权的资源
        remove(Wraps.<SystemRoleAuthority>lbQ().eq(SystemRoleAuthority::getRoleId, saveRoleResourceDTO.getRoleId()));
        //保存新授权的资源
        List<Long> authorityId = saveRoleResourceDTO.getAuthorityId();
        if(!authorityId.isEmpty() && authorityId.size()>0){
            List<SystemRoleAuthority> systemRoleAuthorityList = authorityId.stream().map(id -> {
                SystemRoleAuthority systemRoleAuthority = SystemRoleAuthority.builder().roleId(saveRoleResourceDTO.getRoleId()).authorityId(id).build();
                return systemRoleAuthority;
            }).collect(Collectors.toList());
            saveBatch(systemRoleAuthorityList);
        }
    }
}
